package my.file.xml;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import my.util.HttpRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

public class XmlTest {
	@Test
	public void Test_01(){
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(new File("src/main/java/my/xml/books.xml"));
			Element bookStore = document.getRootElement();
			Element element = bookStore.element("book");
			List elements = bookStore.elements();
			System.out.println(elements);
			System.out.println(element);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void Test_03() throws MalformedURLException, UnsupportedEncodingException{
		SAXReader saxReader = new SAXReader();
		try {
			String sendGet = HttpRequest.sendGet("http://api-test.elong.com/xml/v2.0/hotel/geo_cn.xml");
			ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(sendGet.getBytes());   
//			Document document = saxReader.read(new URL("http://api-test.elong.com/xml/v2.0/hotel/geo_cn.xml"));
			Document document = saxReader.read(byteArrayInputStream);
//			Document document = saxReader.read(new File("src/main/java/my/xml/books.xml"));
			Element bookStore = document.getRootElement();
			Element element = bookStore.element("HotelGeoList");
			List elements = bookStore.elements();
			System.out.println(elements);
			System.out.println(element);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void Test_02(){
		String departure="宁波";
		String destination="上海";
//		String flight_trend_url = "https://ws.qunar.com/holidayService.jcp?lane=departure-destination";
//		String url = flight_trend_url.replace("departure", departure).replace("destination", destination);
		String url="http://api-test.elong.com/xml/v2.0/hotel/geo_cn.xml";
		String sendGet = HttpRequest.sendGet(url);
//		SAXReader saxReader = new SAXReader();
		try {
//			Document read = saxReader.read(sendGet);
			Document read = DocumentHelper.parseText(sendGet);
			System.out.println("0:"+read);
			Element rootElement = read.getRootElement();
			System.out.println("1:"+rootElement);
//			Element airline = rootElement.element("airline");
			Element airline = rootElement.element("HotelGeoList");
			System.out.println("2:"+airline);
			Iterator elementIterator = airline.elementIterator();
			List<Map<String,Object>> prices = new ArrayList<Map<String,Object>>(); 
			while (elementIterator.hasNext()) {
				Element line = (Element) elementIterator.next();
				String date = line.attributeValue("date");
				Iterator iterator = line.elementIterator();
				if(!iterator.hasNext())
					continue;
				Map<String,Object> priceMap=new HashMap<String,Object>();
				priceMap.put("date", date);
				while (iterator.hasNext()) {
					Element client = (Element) iterator.next();
					String type = client.attributeValue("type");
					int price = Integer.parseInt(client.attributeValue("price"));
					type +="_price";
					priceMap.put(type, price);
				}
				prices.add(priceMap);
			}
			System.out.println(prices);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void Test_04(){
		String url="http://api-test.elong.com/xml/v2.0/hotel/geo_cn.xml";
		String sendGet = HttpRequest.sendGet(url);
		System.out.println(sendGet);
		try {
			Document read = DocumentHelper.parseText(sendGet);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
